﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FL.DBModule.Sys;



namespace FL.Bussiness
{
    /// <summary>
    /// ID号
    /// </summary>
    public static class InternalIDMaker
    {
        private static int curNumLength = 9;
        private static string curPreposition = "";
        private static string curPostposition = "";

        public static Int64 GetSequenceNum(string curSequenceName, string type = "normal", string pre_postion = "", string post_postion = "")
        {
            BaseBussnies bll = new BaseBussnies(new t_sys_sequence());
            IList<t_sys_sequence> seqlist = bll.GetEntitys<t_sys_sequence>("name='" + curSequenceName + "'");

            long result = 0;
            long myCurNum = 0;
            int myStep = 0;

            int icount = seqlist == null ? 0 : seqlist.Count();
            if (icount > 0)
            {
                t_sys_sequence seqItem = seqlist.First();
                //if (seqItem.cur_num != null)
                myCurNum = seqItem.cur_num;
                //long.TryParse(seqItem.cur_num,out myCurNum);
                //if (seqItem.step != null)
                myStep = seqItem.step;
                result = myCurNum + myStep;
                if (type == "date")
                {
                    if (seqItem.current_date.ToString("yyyyMMdd") != System.DateTime.Now.ToString("yyyyMMdd"))
                    {
                        seqItem.current_date = System.DateTime.Now;
                        seqItem.start_num = 0;
                        result = 1;
                        seqItem.cur_num = 1;
                    }
                }
                if (result > seqItem.max_num)
                    seqItem.cur_num = seqItem.start_num;
                else
                    seqItem.cur_num = result;


                //if (seqItem.num_length != null)
                curNumLength = (int)seqItem.num_length;

                curPostposition = pre_postion != "" ? pre_postion : seqItem.post_postion;
                curPreposition = post_postion != "" ? post_postion : seqItem.pre_position;
                bll.entity = seqItem;
                bll.Update();
            }
            else
            {
                t_sys_sequence newItem = new t_sys_sequence();
                newItem.name = curSequenceName;
                newItem.code = curSequenceName;
                newItem.type = type;
                newItem.start_num = 1;
                newItem.step = 1;
                newItem.max_num = 999999999;
                newItem.cur_num = 1;
                newItem.pre_position = pre_postion;
                newItem.post_postion = post_postion;
                newItem.current_date = System.DateTime.Now;//设为当日
                //newItem.Preposition = "";
                newItem.num_length = type == "date" ? 4 : 9;//每日重新计数

                if (newItem.cur_num != 0)
                    result = newItem.cur_num;

                if (newItem.num_length != 0)
                    curNumLength = (int)newItem.num_length;
                curPostposition = newItem.post_postion;
                curPreposition = newItem.pre_position;
                bll.entity = newItem;
                bll.Insert();
            }
            return result;
        }

        public static Int64 GetSequenceNum(string curSequenceName, IList<t_sys_sequence> seqlist, string type = "normal", string pre_postion = "", string post_postion = "")
        {

            long result = 0;
            long myCurNum = 0;
            int myStep = 0;
            BaseBussnies bll = new BaseBussnies(new t_sys_sequence());
            int icount = seqlist == null ? 0 : seqlist.Count();
            if (icount > 0)
            {
                t_sys_sequence seqItem = seqlist.First();
                //if (seqItem.cur_num != null)
                if (type == "date")
                {
                    if (seqItem.current_date.ToString("yyyyMMdd") != System.DateTime.Now.ToString("yyyyMMdd"))
                    {
                        seqItem.current_date = System.DateTime.Now;
                        seqItem.start_num = 1;
                        seqItem.cur_num = 1;
                    }
                }

                myCurNum = seqItem.cur_num;
                //long.TryParse(seqItem.cur_num,out myCurNum);
                //if (seqItem.step != null)
                myStep = seqItem.step;
                result = myCurNum + myStep;

                if (result > seqItem.max_num)
                    seqItem.cur_num = seqItem.start_num;
                else
                    seqItem.cur_num = result;


                //if (seqItem.num_length != null)
                    curNumLength = (int)seqItem.num_length;
                curPostposition = seqItem.post_postion;
                curPreposition = seqItem.pre_position;
                bll.entity = seqItem;
                bll.Update();
            }
            else
            {
                t_sys_sequence newItem = new t_sys_sequence();
                newItem.name = curSequenceName;
                newItem.code = curSequenceName;
                newItem.type = type;
                newItem.start_num = 1;
                newItem.step = 1;
                newItem.max_num = 999999999;
                newItem.cur_num = 1;
                newItem.pre_position = "";
                newItem.current_date = System.DateTime.Now;//设为当日
                //newItem.Preposition = "";
                newItem.num_length = type == "date" ? 4 : 9;//每日重新计数
                newItem.add_time = DateTime.Now;
                if (newItem.cur_num != 0)
                    result = newItem.cur_num;

                if (newItem.num_length != 0)
                    curNumLength = (int)newItem.num_length;
                curPostposition = newItem.post_postion;
                curPreposition = newItem.pre_position;
                bll.entity = newItem;
                bll.Insert();
            }
            return result;
        }


        public static string GetFormatSequence(string sequenceName, string preposition, string postposition)
        {
            long curSeqNum = 0;
            string prepNum = "";
            BaseBussnies bll = new BaseBussnies(new t_sys_sequence());
            IList<t_sys_sequence> seqlist = bll.GetEntitys<t_sys_sequence>("name='" + sequenceName + "'");
            curSeqNum = GetSequenceNum(sequenceName, seqlist, "normal", preposition, postposition);
            for (int i = 0; i < (curNumLength - curSeqNum.ToString().Length); i++)
            {
                prepNum += "0";
            }
            return preposition + prepNum + curSeqNum.ToString() + postposition;
        }

        public static string GetFormatSequence(string sequenceName, string type, string preposition = "", string postposition = "")
        {
            Int64 curSeqNum = 0;
            string prepNum = "";
            curSeqNum = GetSequenceNum(sequenceName, type, preposition, postposition);
            for (int i = 0; i < (curNumLength - curSeqNum.ToString().Length); i++)
            {
                prepNum += "0";
            }
            if (type == "date")
            {
                prepNum = System.DateTime.Now.ToString("yyyyMMdd") + prepNum;
            }
            return preposition + prepNum + curSeqNum.ToString() + postposition;
        }

        public static string GetSequenceStr(string sequenceName)
        {
            Int64 curSeqNum = 0;
            string prepNum = "";
            curSeqNum = GetSequenceNum(sequenceName);
            for (int i = 0; i < (curNumLength - curSeqNum.ToString().Length); i++)
            {
                prepNum += "0";
            }
            return curPreposition + prepNum + curSeqNum.ToString() + curPostposition;
        }

    }

    public class IDMaker
    {
        public static Int64 GetSequenceNum(string curSequenceName)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetSequenceNum(curSequenceName);
            }
        }

        public static Int64 GetSequenceNum(string curSequenceName, string type)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetSequenceNum(curSequenceName, type);
            }
        }

        public static Int64 GetSequenceNum(string curSequenceName, string type, string pre_postion, string post_postion)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetSequenceNum(curSequenceName, type, pre_postion, post_postion);
            }
        }

        public static string GetFormatSequence(string sequenceName, string preposition, string postposition)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetFormatSequence(sequenceName, preposition, postposition);
            }
        }

        public static string GetSequenceStr(string sequenceName, string type = "", string preposition = "", string postposition = "")
        {
            lock (typeof(InternalIDMaker))
            {
                if (type == "date")
                {
                    return InternalIDMaker.GetFormatSequence(sequenceName, type, preposition, postposition);
                }
                else
                {
                    return InternalIDMaker.GetFormatSequence(sequenceName, preposition, postposition);
                }
            }
        }

    }
}

